$(function () {
    getUserInfo()
    $('#logout').on('click', function () {
        //eg1
        layer.confirm('确认退出登录吗？', { icon: 3, title: '提示' }, function (index) {
            location.href = '/login.html'
            localStorage.removeItem('token')
            // 关闭询问框
            layer.close(index);
        });

    })
})
// 渲染头像和欢迎信息
function getUserInfo() {
    axios({
        method: 'GET',
        url: '/my/userinfo',
    }).then(res => {
        if (res.data.status !== 0) {
            return layer.msg(res.data.message)
        }
        renderAvatar(res.data.data)
    })
}
function renderAvatar(user) {
    //渲染文字欢迎信息
    let name = user.nickname || user.username
    $('.welcome').html('欢迎 ' + name)
    // 渲染头像信息
    if (user.user_pic !== null) {
        $('.layui-nav-img').show().prop('src', user.user_pic)
        $('.avatar-text').hide()
    } else {
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)
    }

}